﻿using cuiail.DBConnections;
using cuiail.DBModels;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Routing;
using System.Data;

namespace cuiail.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class LabController : ControllerBase
    {
        private readonly IWorkingModeService _workingModeService;
        private readonly ICurrentWorkModeService _currentWorkModeService;
        private readonly IPrintContentService _printContentService;
        private readonly ILogger<LabController> _logger;

        public LabController(IWorkingModeService workingModeService,ICurrentWorkModeService currentWorkModeService, IPrintContentService printContentService, ILogger<LabController> logger)
        {
            _currentWorkModeService = currentWorkModeService;
            _workingModeService = workingModeService;
            _printContentService= printContentService;
            _logger = logger;
        }

        /// <summary>
        /// 获取所有工作模式
        /// </summary>
        [HttpGet]
        [Route("GetWorkingModes")]
        public async Task<ActionResult<IEnumerable<WorkingMode>>> GetWorkingModesAsync()
        {
            try
            {
                var result = await _workingModeService.GetAllWorkingModesAsync();
                return Ok(result);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "查询工作模式时发生错误");
                return StatusCode(500, "查询数据时发生错误，请稍后重试");
            }
        }

        /// <summary>
        /// 获取当前工作模式
        /// </summary>
        [HttpGet]
        [Route("GetCurrentWorkingMode")]
        public async Task<ActionResult<CurrentWorkingMode2>> GetCurrentWorkingModeAsync()
        {
            try
            {
                var result = await _currentWorkModeService.GetCurrentWorkingModeAsync();
                return Ok(result);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "查询工作模式时发生错误");
                return StatusCode(500, "查询数据时发生错误，请稍后重试");
            }
        }

        /// <summary>
        /// 写入当前工作模式
        /// </summary>
        /// <param name="currentWorkingMode">当前工作模式代码</param>
        /// <returns>写入成功或者失败</returns>
        [HttpPost]
        [Route("InsertCurrentWorkingMode")]
        public async Task<ActionResult<bool>> InsertCurrentWorkingModeAsync(int currentWorkingMode)
        {
            try
            {
                var result = await _currentWorkModeService.InsertCurrentWorkingModeAsync(currentWorkingMode);
                return Ok(result);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "写入当前工作模式时发生错误");
                return StatusCode(500, "写入当前工作模式时发生错误，请稍后重试");
            }
        }


        /// <summary>
        /// 获取当前未打印内容
        /// </summary>
        [HttpGet]
        [Route("GetUnPrintContentAsync")]
        public async Task<ActionResult<IEnumerable<PrintContent>>> GetUnPrintContentAsync()
        {
            try
            {
                var result = await _printContentService.GetUnPrintContentAsync();
                return Ok(result);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "查询工作模式时发生错误");
                return StatusCode(500, "查询数据时发生错误，请稍后重试");
            }
        }


        /// <summary>
        /// 写入新的打印任务
        /// </summary>
        /// <param name="printContentInfo"></param>
        /// <returns>写入结果</returns>
        [HttpPost]
        [Route("InsertPrintContentInfo")]
        public async Task<ActionResult<bool>> InsertPrintContentInfoAsync(string printContentInfo)
        {
            try
            {
                var result = await _printContentService.InsertPrintContentInfoAsync(printContentInfo);
                return Ok(result);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "写入当前打印内容时发生错误");
                return StatusCode(500, "写入打印内容时发生错误，请稍后重试");
            }
        }


        /// <summary>
        /// 打印完成后更新打印任务信息(汇报打印完成)
        /// 只需传入PrintContentID和IsPrintFinish=true，FinishTime有默认当前时间，其余字段不使用
        /// </summary>
        /// <param name="printContent"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("UpdatePrintContent")]
        public async Task<ActionResult<bool>> UpdatePrintContentAsync(PrintContent printContent)
        {
            try
            {
                var result = await _printContentService.UpdatePrintContentAsync(printContent);
                return Ok(result);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "写入当前打印内容时发生错误");
                return StatusCode(500, "写入打印内容时发生错误，请稍后重试");
            }
        }
    }
}
